﻿using System;
using System.Text;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Layout;
using Avalonia.Media;
using MusicFluent.Models;
using MusicFluent.ViewModels;
using MusicFluent.Views;
using NetEasyMusic.Lib.Data;
using NetEasyMusic.Lib.Models;

namespace MusicFluent.MusicViews;

public partial class YearReportView : UserControl
{
    public YearReportView()
    {
        InitializeComponent();
    }


    protected override async void OnInitialized()
    {
        if (DataContext is not ReportModel _model) return;
        var reg = DateTimeOffset.FromUnixTimeMilliseconds(long.Parse(_model.RegTime));
        reg = reg.AddHours(8);
        RegBlock.Text = reg.ToString("yyyy年M月d日 dddd") + "，您第一次来到了这里";
        NowBlock.Text = $"一转眼,{Environment.NewLine}已经过去了{DateTime.Today.Year - reg.Year}年";
        var builder = new StringBuilder();
        foreach (var rank in _model.GenreRanks)
            builder.Append($"{rank.Percent}*,");
        GenreRankGrid.ColumnDefinitions =
            ColumnDefinitions.Parse(builder.ToString()[..^1]);
        var r = new Random();
        var colors = new[] { "#ee2746", "#806d9e", "#12aa9c", "#533c1b" };
        for (var i = 0; i < _model.GenreRanks.Length; i++)
        {
            var control = new Border()
            {
                Child = new TextBlock()
                {
                    Text = _model.GenreRanks[i].GenreTagName, HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Center, FontSize = 22, FontWeight = FontWeight.SemiBold,
                    TextWrapping = TextWrapping.WrapWithOverflow, MaxLines = 1
                },
                Height = 200,
                Background = new SolidColorBrush(Color.Parse(colors[r.Next(colors.Length)]))
            };
            GenreRankGrid.Children.Add(control);
            Grid.SetColumn(control, i);
        }

        using var album = new Album();
        AnnualAlbumItems.ItemsSource = await album.GetAlbumSongs(_model.AnnualAlbum);
        AnnualSingerSongItems.ItemsSource = await album.GetTopSongsAndPic(_model.AnnualSingerSongs);
    }

    private async void KeywordSongClick(object? sender, RoutedEventArgs e)
    {
        if (sender is not Control control) return;
        if (control.DataContext is not LyricListItem item) return;
        var view = ViewOpera.GetView<MainWindow>(this);
        if (view == null) return;
        using var music = new Music();
        var songFormId = await music.GetSongFormId(item.SongId);
        view.SongPanel.DataContext = songFormId;
        if (view.DataContext is not MainWindowViewModel model) return;
        model.PlayChange(songFormId.Id);
    }

    private async void YearPlayClick(object? sender, RoutedEventArgs e)
    {
        var view = ViewOpera.GetView<MainWindow>(Parent);
        if (view == null) return;
        if (DataContext is not ReportModel _model) return;
        using var music = new Music(view.Cookie);
        var model = await music.GetSongFormId(_model.YearSongId);
        view.SongPanel.DataContext = model;
        if (view.DataContext is not MainWindowViewModel dataContext) return;
        dataContext.Add(ViewOpera.GetList(this));
        dataContext.PlayChange(model.Id);
    }

    private async void SeasonSongClick(object? sender, RoutedEventArgs e)
    {
        var view = ViewOpera.GetView<MainWindow>(Parent);
        if (view == null) return;
        if (sender is not Control control) return;
        var contentControl = ViewOpera.GetView<ContentControl>(control);
        if (contentControl?.Content is not SeasonListen _model) return;
        using var music = new Music();
        var model = await music.GetSongFormId(_model.SongId);
        view.SongPanel.DataContext = model;
        if (view.DataContext is not MainWindowViewModel dataContext) return;
        dataContext.PlayChange(model.Id);
    }

    private void SeasonArtistClick(object? sender, RoutedEventArgs e)
    {
        if (sender is not Control control) return;
        var contentControl = ViewOpera.GetView<ContentControl>(control);
        if (contentControl?.Content is not SeasonListen model) return;
        var view = ViewOpera.GetView<MainWindow>(Parent as Control);
        if (view == null) return;
        var artist = new ArtistView(new ArtistModel() { Id = model.ArtistId, Name = model.ArtistName }, view.Cookie);
        view.Navigate(artist);
    }

    private async void AnnualSongClick(object? sender, RoutedEventArgs e)
    {
        var view = ViewOpera.GetView<MainWindow>(Parent);
        if (view == null) return;
        if (DataContext is not ReportModel _model) return;
        using var music = new Music(view.Cookie);
        var model = await music.GetSongFormId(_model.AnnualSongId);
        view.SongPanel.DataContext = model;
        if (view.DataContext is not MainWindowViewModel dataContext) return;
        dataContext.PlayChange(model.Id);
    }
}